{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在连接客户端: C:\\同花顺软件\\同花顺\\xiadan.exe ......\n",
      "连接成功!!!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\anconda3\\lib\\site-packages\\pywinauto\\application.py:1085: UserWarning: 32-bit application should be automated using 32-bit Python (you use 64-bit Python)\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from real_trade_ths import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "captcha result--> 4028\n",
      "点击登入\n"
     ]
    }
   ],
   "source": [
    "check_pos('A50549') \n",
    "# check_history_order('A50549')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from THS.THSTrader import THSTrader\n",
    "trader = THSTrader(r\"C:\\同花顺软件\\同花顺\\xiadan.exe\") \n",
    "\n",
    "account='A50549'\n",
    "\n",
    "st = time.time()\n",
    "conn = pymysql.connect(host=MYSQL_HOST_M, user=MYSQL_USER, passwd=MYSQL_PASS, db=MYSQL_DB, port=MYSQL_PORT, charset='utf8')\n",
    "cursor = conn.cursor()  \n",
    "\n",
    "nowdatetime=int(time.strftime('%H%M'))\n",
    "pos_df= pd.read_sql(f\"select * from `yxk_realtrade_pos` where  account='{account}'  and trade_date=CURDATE()  and pos>0 ;\", con=conn)\n",
    "pos_df.set_index('stock_id',inplace=True)\n",
    "\n",
    "cal_pos_stock_list=list(set(pos_df.index))\n",
    "cal_pos_stock_list=[str(i).zfill(6) for i in cal_pos_stock_list]\n",
    "\n",
    "\n",
    "upsql0= f\"delete from  yxk_realtrade_pos_qry where account='{account}'\"\n",
    "cursor.execute(upsql0)\n",
    "conn.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "captcha result--> 2330\n",
      "点击登入\n"
     ]
    }
   ],
   "source": [
    "# pos_df.index=pos_df.index.astype('int')\n",
    "position=trader.get_position()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[65, 2484, 2846, 300226, 300564, 300862, 300875, 300898, 300928, 300949, 300980, 301098, 600257, 600602, 600970, 603000, 603223, 603918, 605117]\n"
     ]
    }
   ],
   "source": [
    "print(list(pos_df.index.astype('int')))\n",
    "\n",
    "if '证券名称' not in position[0].keys():\n",
    "    logger.info('got error check_pos')\n",
    "\n",
    "for i in range(len(position)):\n",
    "\n",
    "\n",
    "    stock_name=position[i]['证券名称']\n",
    "    stock_id=position[i]['证券代码']\n",
    "    stock_id_str=str(stock_id).zfill(6)\n",
    "    stock_pos=int(position[i]['股票余额'])\n",
    "    now_price=position[i]['市价']\n",
    "    now_asset=position[i]['市值']\n",
    "    earn_money=position[i]['盈亏']\n",
    "    avg_price=position[i]['成本价']\n",
    "    frozen_pos=position[i]['冻结数量']\n",
    "    avg_increace=position[i]['盈亏比例(%)']\n",
    "    if stock_id_str in cal_pos_stock_list:\n",
    "        cal_pos_stock_list.remove(stock_id_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "cal_pos_stock_list=list(set(pos_df.index))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'明细': '',\n",
       "  '证券代码': 2436,\n",
       "  '证券名称': '兴森科技',\n",
       "  '股票余额': 600,\n",
       "  '可用余额': 600,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -837.33,\n",
       "  '成本价': 17.316,\n",
       "  '盈亏比例(%)': -8.06,\n",
       "  '市价': 15.92,\n",
       "  '市值': 9552.0,\n",
       "  '市场代码': 1,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00106169972',\n",
       "  '实际数量': 600,\n",
       "  '可申赎数量': 600,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 2903,\n",
       "  '证券名称': '宇环数控',\n",
       "  '股票余额': 400,\n",
       "  '可用余额': 400,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -366.82,\n",
       "  '成本价': 22.006999999999998,\n",
       "  '盈亏比例(%)': -4.17,\n",
       "  '市价': 21.09,\n",
       "  '市值': 8436.0,\n",
       "  '市场代码': 1,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00106169972',\n",
       "  '实际数量': 400,\n",
       "  '可申赎数量': 400,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 2947,\n",
       "  '证券名称': '恒铭达',\n",
       "  '股票余额': 300,\n",
       "  '可用余额': 300,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -129.1,\n",
       "  '成本价': 32.36,\n",
       "  '盈亏比例(%)': -1.33,\n",
       "  '市价': 31.93,\n",
       "  '市值': 9579.0,\n",
       "  '市场代码': 1,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00106169972',\n",
       "  '实际数量': 300,\n",
       "  '可申赎数量': 300,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 3005,\n",
       "  '证券名称': '竞业达',\n",
       "  '股票余额': 100,\n",
       "  '可用余额': 100,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -205.15,\n",
       "  '成本价': 36.062,\n",
       "  '盈亏比例(%)': -5.69,\n",
       "  '市价': 34.01,\n",
       "  '市值': 3401.0,\n",
       "  '市场代码': 1,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00106169972',\n",
       "  '实际数量': 100,\n",
       "  '可申赎数量': 100,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 300025,\n",
       "  '证券名称': '华星创业',\n",
       "  '股票余额': 200,\n",
       "  '可用余额': 200,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -66.68,\n",
       "  '成本价': 10.703,\n",
       "  '盈亏比例(%)': -3.11,\n",
       "  '市价': 10.37,\n",
       "  '市值': 2074.0,\n",
       "  '市场代码': 1,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00106169972',\n",
       "  '实际数量': 200,\n",
       "  '可申赎数量': 200,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 300136,\n",
       "  '证券名称': '信维通信',\n",
       "  '股票余额': 200,\n",
       "  '可用余额': 200,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': 30.5,\n",
       "  '成本价': 23.448,\n",
       "  '盈亏比例(%)': 0.65,\n",
       "  '市价': 23.6,\n",
       "  '市值': 4720.0,\n",
       "  '市场代码': 1,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00106169972',\n",
       "  '实际数量': 200,\n",
       "  '可申赎数量': 200,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 300548,\n",
       "  '证券名称': '博创科技',\n",
       "  '股票余额': 400,\n",
       "  '可用余额': 400,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -367.68,\n",
       "  '成本价': 28.719,\n",
       "  '盈亏比例(%)': -3.2,\n",
       "  '市价': 27.8,\n",
       "  '市值': 11120.0,\n",
       "  '市场代码': 1,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00106169972',\n",
       "  '实际数量': 400,\n",
       "  '可申赎数量': 400,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 300551,\n",
       "  '证券名称': '古鳌科技',\n",
       "  '股票余额': 300,\n",
       "  '可用余额': 300,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -485.71,\n",
       "  '成本价': 28.199,\n",
       "  '盈亏比例(%)': -5.74,\n",
       "  '市价': 26.58,\n",
       "  '市值': 7974.0,\n",
       "  '市场代码': 1,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00106169972',\n",
       "  '实际数量': 300,\n",
       "  '可申赎数量': 300,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 300763,\n",
       "  '证券名称': '锦浪科技',\n",
       "  '股票余额': 100,\n",
       "  '可用余额': 100,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -120.29,\n",
       "  '成本价': 71.633,\n",
       "  '盈亏比例(%)': -1.68,\n",
       "  '市价': 70.43,\n",
       "  '市值': 7043.0,\n",
       "  '市场代码': 1,\n",
       "  '交易市场': '深圳Ａ股',\n",
       "  '股东帐户': '00106169972',\n",
       "  '实际数量': 100,\n",
       "  '可申赎数量': 100,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 600785,\n",
       "  '证券名称': '新华百货',\n",
       "  '股票余额': 100,\n",
       "  '可用余额': 100,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': 213.73,\n",
       "  '成本价': 12.302999999999999,\n",
       "  '盈亏比例(%)': 17.37,\n",
       "  '市价': 14.44,\n",
       "  '市值': 1444.0,\n",
       "  '市场代码': 2,\n",
       "  '交易市场': '上海Ａ股',\n",
       "  '股东帐户': 'A481890096',\n",
       "  '实际数量': 100,\n",
       "  '可申赎数量': 100,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 601858,\n",
       "  '证券名称': '中国科传',\n",
       "  '股票余额': 300,\n",
       "  '可用余额': 300,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -912.1,\n",
       "  '成本价': 32.31,\n",
       "  '盈亏比例(%)': -9.41,\n",
       "  '市价': 29.27,\n",
       "  '市值': 8781.0,\n",
       "  '市场代码': 2,\n",
       "  '交易市场': '上海Ａ股',\n",
       "  '股东帐户': 'A481890096',\n",
       "  '实际数量': 300,\n",
       "  '可申赎数量': 300,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 603203,\n",
       "  '证券名称': '快克智能',\n",
       "  '股票余额': 300,\n",
       "  '可用余额': 300,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': 39.14,\n",
       "  '成本价': 29.77,\n",
       "  '盈亏比例(%)': 0.44,\n",
       "  '市价': 29.9,\n",
       "  '市值': 8970.0,\n",
       "  '市场代码': 2,\n",
       "  '交易市场': '上海Ａ股',\n",
       "  '股东帐户': 'A481890096',\n",
       "  '实际数量': 300,\n",
       "  '可申赎数量': 300,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 603286,\n",
       "  '证券名称': '日盈电子',\n",
       "  '股票余额': 400,\n",
       "  '可用余额': 400,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': 848.97,\n",
       "  '成本价': 23.648000000000003,\n",
       "  '盈亏比例(%)': 8.98,\n",
       "  '市价': 25.77,\n",
       "  '市值': 10308.0,\n",
       "  '市场代码': 2,\n",
       "  '交易市场': '上海Ａ股',\n",
       "  '股东帐户': 'A481890096',\n",
       "  '实际数量': 400,\n",
       "  '可申赎数量': 400,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 603353,\n",
       "  '证券名称': '和顺石油',\n",
       "  '股票余额': 200,\n",
       "  '可用余额': 200,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': 392.01,\n",
       "  '成本价': 31.14,\n",
       "  '盈亏比例(%)': 6.29,\n",
       "  '市价': 33.1,\n",
       "  '市值': 6620.0,\n",
       "  '市场代码': 2,\n",
       "  '交易市场': '上海Ａ股',\n",
       "  '股东帐户': 'A481890096',\n",
       "  '实际数量': 200,\n",
       "  '可申赎数量': 200,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 603615,\n",
       "  '证券名称': '茶花股份',\n",
       "  '股票余额': 200,\n",
       "  '可用余额': 200,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': 507.16,\n",
       "  '成本价': 13.154000000000002,\n",
       "  '盈亏比例(%)': 19.28,\n",
       "  '市价': 15.69,\n",
       "  '市值': 3138.0,\n",
       "  '市场代码': 2,\n",
       "  '交易市场': '上海Ａ股',\n",
       "  '股东帐户': 'A481890096',\n",
       "  '实际数量': 200,\n",
       "  '可申赎数量': 200,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 603666,\n",
       "  '证券名称': '亿嘉和',\n",
       "  '股票余额': 100,\n",
       "  '可用余额': 100,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -134.12,\n",
       "  '成本价': 35.010999999999996,\n",
       "  '盈亏比例(%)': -3.83,\n",
       "  '市价': 33.67,\n",
       "  '市值': 3367.0,\n",
       "  '市场代码': 2,\n",
       "  '交易市场': '上海Ａ股',\n",
       "  '股东帐户': 'A481890096',\n",
       "  '实际数量': 100,\n",
       "  '可申赎数量': 100,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''},\n",
       " {'明细': '',\n",
       "  '证券代码': 603985,\n",
       "  '证券名称': '恒润股份',\n",
       "  '股票余额': 100,\n",
       "  '可用余额': 100,\n",
       "  '冻结数量': 0,\n",
       "  '盈亏': -104.28,\n",
       "  '成本价': 39.873000000000005,\n",
       "  '盈亏比例(%)': -2.62,\n",
       "  '市价': 38.83,\n",
       "  '市值': 3883.0,\n",
       "  '市场代码': 2,\n",
       "  '交易市场': '上海Ａ股',\n",
       "  '股东帐户': 'A481890096',\n",
       "  '实际数量': 100,\n",
       "  '可申赎数量': 100,\n",
       "  '资讯': '',\n",
       "  'Unnamed: 17': ''}]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "position"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'builtin_function_or_method' object is not iterable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-1e5de8dd87b7>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0morder_dict\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0morder_dict\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'key1'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m123\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0morder_dict\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'builtin_function_or_method' object is not iterable"
     ]
    }
   ],
   "source": [
    "order_dict={}\n",
    "order_dict['key1']=123\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'key1' in order_dict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
